﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;

namespace V5_Common {
    /// <summary>
    /// 单个接点xml操作
    /// </summary>
    public class XmlSetHelper {
        // Fields
        private string m_filename;
        private Hashtable m_items;
        private string m_rootNodeName;
        #region 属性
        public string FileName {
            get {
                return this.m_filename;
            }
            set {
                this.m_filename = value;
            }
        }

        public Hashtable Items {
            get {
                return this.m_items;
            }
            set {
                this.m_items = value;
            }
        }

        public string RootNodeName {
            get {
                return this.m_rootNodeName;
            }
            set {
                this.m_rootNodeName = value;
            }
        }
        #endregion
        // Methods
        public XmlSetHelper() {
            this.m_items = new Hashtable(10);
            this.m_rootNodeName = "appsettings";
            this.m_filename = AppDomain.CurrentDomain.BaseDirectory + "MyConfig.config";
            this.LoadDom();
        }

        public XmlSetHelper(string filename) {
            this.m_items = new Hashtable(10);
            this.m_rootNodeName = "appsettings";
            this.m_filename = AppDomain.CurrentDomain.BaseDirectory + filename;
            this.LoadDom();
        }

        public XmlSetHelper(string filename, string rootNodeName) {
            this.m_items = new Hashtable(10);
            this.m_filename = AppDomain.CurrentDomain.BaseDirectory + filename;
            this.m_rootNodeName = rootNodeName;
            this.LoadDom();
        }
        /// <summary>
        /// 载入
        /// </summary>
        public void LoadDom() {
            XmlDocument document = new XmlDataDocument();
            try {
                document.Load(this.FileName);
                XmlNodeList childNodes = document.SelectSingleNode(this.RootNodeName.Trim()).ChildNodes;
                foreach (XmlNode node in childNodes) {
                    XmlElement element = (XmlElement)node;
                    if (!this.m_items.Contains(element.Name.ToLower())) {
                        this.m_items.Add(element.Name.ToLower(), element.InnerText);
                    }
                }
            }
            catch {
                document = null;
                //throw new Exception();
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        public void SaveDom() {
            XmlDocument document = new XmlDocument();
            XmlElement newChild = document.CreateElement(this.RootNodeName);
            document.AppendChild(newChild);
            IDictionaryEnumerator enumerator = this.Items.GetEnumerator();
            while (enumerator.MoveNext()) {
                DictionaryEntry current = (DictionaryEntry)enumerator.Current;
                XmlElement element2 = document.CreateElement(Convert.ToString(current.Key));
                element2.InnerText = Convert.ToString(current.Value);
                newChild.AppendChild(element2);
            }
            document.Save(this.FileName);
        }

    }
}
